package cn.fl.riskctrl.ccard.service.impl;

import cmm.mid.core.framework.dto.BaseParamDTO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.logger.eum.LogExceptionEnum;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.riskctrl.ccard.service.ICcardV2ResultService;
import cn.fl.riskctrl.transform.RcCcardv2CstResultVOTransform;
import cn.fl.riskctrl.transform.RcCcardv2PrjResultVOTransform;
import cn.fl.riskctrl.transform.RcCcardv2RiskResultTransform;
import cn.fl.riskctrl.vo.RcCcardv2CstResultVO;
import cn.fl.riskctrl.vo.RcCcardv2PrjResultVO;
import cn.fl.riskctrl.vo.RcCcardv2RiskResultVO;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import fl.riskctrl.facade.externalinterface.service.api.IExternalCcardV2ResultApiService;
import fl.riskctrl.facade.riskctrl.ccardv2.dto.RcCcardv2CstResultViewDTO;
import fl.riskctrl.facade.riskctrl.ccardv2.dto.RcCcardv2PrjResultViewDTO;
import fl.riskctrl.facade.riskctrl.common.dto.RcVectorResultDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CcardV2ResultServiceImpl implements ICcardV2ResultService {
    @Autowired
    private IExternalCcardV2ResultApiService externalCcardV2ResultApiService;

    @Override
    public Pager getCcardV2CstResultList(Pager page, Long cstId) throws BaseException {
        BaseLogUtils.newLogger("获取C卡V2客户：{}的客户列表信息开始", cstId).info();

        Pager pager =null;

        try {
            BaseParamDTO baseParamDTO = new BaseParamDTO();
            DataResultDTO result =externalCcardV2ResultApiService.getCcardV2CstInfoDTOListPager(page, cstId, baseParamDTO);
            if(null==result || result.getSuccess()!= BaseConstant.IS_YESNO_YES){
                BaseLogUtils.newLogger("获取C卡V2客户信息失败：{}", JSONObject.toJSONString(result)).error(LogExceptionEnum.COMMON_CODE_0002);
                throw new BaseException("获取C卡V2客户信息失败！");
            }

            if(null != result.getData()) {
                pager = (Pager)result.getData();

                List<RcCcardv2CstResultViewDTO> resultList = (List<RcCcardv2CstResultViewDTO>) pager.getResultList();
                List<RcCcardv2CstResultVO> list = RcCcardv2CstResultVOTransform.toVOList(resultList);

                pager.setResultList(list);
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("获取C卡V2客户：{}的客户列表信息失败：{}", cstId, e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡V2客户：{}的客户列表信息失败：{}", cstId, e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw new BaseException("获取C卡V2客户的客户列表信息失败！");
        } finally {
            BaseLogUtils.newLogger("获取C卡V2客户：{}的客户列表信息结束：{}", cstId, JSONObject.toJSONString(pager)).info();
        }

        return pager;
    }

    @Override
    public Pager getCcardV2PrjResultList(Pager page, Long cstId, String batchNo) throws BaseException {
        BaseLogUtils.newLogger("获取C卡V2客户：{}，batchNo：{}的项目列表信息开始", cstId, batchNo).info();

        Pager pager =null;
        try {
            BaseParamDTO baseParamDTO = new BaseParamDTO();
            DataResultDTO result =externalCcardV2ResultApiService.getCcardV2PrjInfoDTOListPager(page, cstId, batchNo, baseParamDTO);
            if(null==result || result.getSuccess()!= BaseConstant.IS_YESNO_YES){
                BaseLogUtils.newLogger("获取C卡V2客户的项目列表信息失败：{}", JSONObject.toJSONString(result)).error(LogExceptionEnum.COMMON_CODE_0002);
                throw new BaseException("获取C卡V2客户的项目列表信息失败！");
            }

            if(null != result.getData()) {
                pager = (Pager)result.getData();

                List<RcCcardv2PrjResultViewDTO> resultList = (List<RcCcardv2PrjResultViewDTO>) pager.getResultList();
                List<RcCcardv2PrjResultVO> list = RcCcardv2PrjResultVOTransform.toVOList(resultList);

                if(CollectionUtils.isNotEmpty(list)) {
                    for (RcCcardv2PrjResultVO vo : list) {
                        String actPapSchNos = vo.getActPaySchNo();
                        if (StringUtils.isEmpty(actPapSchNos)) {
                            continue;
                        }

                        String[] nos = actPapSchNos.split(";");
                        List<String> datas = new ArrayList<String>();
                        List<List<String>> pdatas = Lists.partition(datas, 4);
                        for (List<String> paySchNos : pdatas) {
                            for (String no : paySchNos) {
                                actPapSchNos = actPapSchNos + no + ";";
                            }
                            actPapSchNos = actPapSchNos + "<br/>";
                        }

                        vo.setActPaySchNo(actPapSchNos);
                    }
                }

                pager.setResultList(list);
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("获取C卡V2客户：{}，batchNo：{}的项目列表信息失败：{}",
                    cstId, batchNo, e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡V2客户：{}，batchNo：{}的项目列表信息失败：{}",
                    cstId, batchNo, e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw new BaseException("获取C卡V2客户的项目列表信息失败！");
        } finally {
            BaseLogUtils.newLogger("获取C卡V2客户：{}，batchNo：{}的项目列表信息结束：{}",
                    cstId, batchNo, JSONObject.toJSONString(pager)).info();
        }

        return pager;
    }

    @Override
    public Pager getCcardV2RiskResultList(Pager page, Long rcCcardv2CstInfoId) throws BaseException {
        BaseLogUtils.newLogger("获取C卡V2客户rcCcardv2CstInfoId：{}的矢量信息开始", rcCcardv2CstInfoId).info();
        String msg = "";
        Pager pager =null;

        try {
            BaseParamDTO baseParamDTO = new BaseParamDTO();
            DataResultDTO result = externalCcardV2ResultApiService.getCcardV2RiskInfoDTOListPager(page, rcCcardv2CstInfoId, baseParamDTO);
            if(null==result || result.getSuccess()!= BaseConstant.IS_YESNO_YES){
                BaseLogUtils.newLogger("获取C卡V2客户的矢量信息失败：{}", JSONObject.toJSONString(result)).error(LogExceptionEnum.COMMON_CODE_0002);
                throw new BaseException("获取C卡V2客户的矢量信息失败！");
            }

            if(null != result.getData()) {
                pager = (Pager)result.getData();

                List<RcVectorResultDTO> resultList = (List<RcVectorResultDTO>) pager.getResultList();
                List<RcCcardv2RiskResultVO> list = RcCcardv2RiskResultTransform.toVOList(resultList);

                pager.setResultList(list);
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("获取C卡V2客户rcCcardv2CstInfoId：{}的矢量信息失败：{}",
                    rcCcardv2CstInfoId, e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡V2客户rcCcardv2CstInfoId：{}的矢量信息失败：{}",
                    rcCcardv2CstInfoId, e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw new BaseException("获取C卡V2客户的矢量信息失败！");
        }

        return pager;
    }

    @Override
    public List<String[]> getCcardV2BrcjwsRiskResultList(Long rcCcardv2CstInfoId) throws BaseException {
        List<String[]> result = new ArrayList<String[]>();
        try {
            DataResultDTO resultDTO = externalCcardV2ResultApiService.getCcardV2BrcjwsRiskInfoDTOList(rcCcardv2CstInfoId, new BaseParamDTO());
            if(null!=resultDTO && null!=resultDTO.getData()) {
                List<RcVectorResultDTO> resultList = (List<RcVectorResultDTO>) resultDTO.getData();
                for(RcVectorResultDTO dto: resultList) {
                    String caseStr = dto.getVectorVal();
                    if(StringUtils.isEmpty(caseStr)){
                        continue;
                    }

                    String[] caseValue = new String[4];
                    JSONObject obj = JSONObject.parseObject(caseStr);
                    caseValue[0] = obj.getString("caseNo");
                    caseValue[1] = obj.getString("caseCause");
                    caseValue[2] = obj.getString("sortTime");
                    caseValue[3] = obj.getString("eventLevel");

                    result.add(caseValue);
                }
            }
        } catch (BaseException e) {
            BaseLogUtils.newLogger("获取C卡V2，案件信息失败：{}", e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw e;
        } catch (Exception e) {
            BaseLogUtils.newLogger("获取C卡V2，案件信息失败:{}", e.getMessage()).error(LogExceptionEnum.COMMON_CODE_0002, e);
            throw new BaseException("获取C卡V2，案件信息失败");
        }

        return result;
    }
}
